//
//  YHContributionAreaVC.m
//  Catches
//
//  Created by jiangqiong on 2018/5/17.
//  Copyright © 2018年 solot. All rights reserved.
//

#import "YHContributionAreaVC.h"
#import "YHContributionCell.h"
#import "YHContributionHeaderView.h"
#import "YHContributionerModel.h"
#import "YHContributionFooterView.h"
#import "YHContributionerListAPI.h"



@interface YHContributionAreaVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong)UITableView * tableView;
@property (nonatomic , strong)YHContributionHeaderView * headerView;
@property (nonatomic , strong)YHContributionFooterView * footerView;
@property (nonatomic , strong)NSMutableArray * dataSource;

@property (nonatomic , strong)YHContributionerModel * myRank;
@property (nonatomic , strong)YHContributionerModel * topRank;
@end


@implementation YHContributionAreaVC

@synthesize tableView = _tableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
    [self loadData];
}


-(void)configUI{
    self.view.backgroundColor = COLOR_WITH_HEX(0xf6f6f6);
    self.tableView.backgroundColor = COLOR_WITH_HEX(0xf6f6f6);
    
    [self.view addSubview:self.footerView];
    [self.view addSubview:self.tableView];
    
    
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.offset(0);
        make.height.mas_equalTo(150);
    }];
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.offset(0);
        make.bottom.mas_equalTo(self.footerView.mas_top).offset(0);
    }];

}

-(void)loadData{
    hq_weak(self);
    YHContributionerListAPI * api = [YHContributionerListAPI new];
    [api setCompleteHandler:^(NSDictionary *result, NSError *error) {
        if(!error){
            hq_strong(self);
            NSMutableArray * arr = result[@"/data/datas"];
            self.myRank = result[@"/data/myRank"];
            if(arr.count > 0){
                self.topRank = arr.firstObject;
                self.dataSource =[NSMutableArray arrayWithArray:[arr subarrayWithRange:NSMakeRange(1, arr.count - 1)]] ;
            }
            
            if(self.myRank){
                for(YHContributionerModel * model in arr){
                    if([model.userno isEqualToString:self.myRank.userno]){
                        self.myRank = nil;
                        break;
                    }
                    
                }
            }
            [self freshUI];
        }
    }];
    
    [api start];
    
    
}

#pragma mark - 逻辑处理
-(void)freshUI{
    [self.tableView reloadData];
    
    if(self.myRank){
        self.myRank.areaType = YHContributionAreaType_world;
        [self.footerView installModel:self.myRank];
        
        self.footerView.hidden = NO;
        [self.footerView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.bottom.offset(0);
            make.height.mas_equalTo(150);
        }];
    }else{
        self.footerView.hidden = YES;
        [self.footerView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.bottom.offset(0);
            make.height.mas_equalTo(0);
        }];
    }
    
    if(self.topRank){
        self.topRank.areaType = YHContributionAreaType_area;
        [self.headerView installData:self.topRank];
    }
   
}


#pragma mark UITableViewDelegate, UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.dataSource.count) {
        YHContributionCell * cell = [tableView dequeueReusableCellWithIdentifier:[YHContributionCell description]];
        YHContributionerModel * model = [self.dataSource objectAtIndex:indexPath.row];
        model.rank = indexPath.row + 2;
        model.areaType = YHContributionAreaType_area;
        [cell installModel:model];
        return cell;
    }
    return [UITableViewCell new];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}



#pragma mark --- lazy load
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[YHContributionCell class] forCellReuseIdentifier:[YHContributionCell description]];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableHeaderView = self.headerView;
    }
    return _tableView;
}

-(YHContributionHeaderView *)headerView{
    if(_headerView == nil){
        _headerView = [[YHContributionHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 190)];
    }
    
    return _headerView;
}

-(YHContributionFooterView *)footerView{
    if(_footerView == nil){
        _footerView = [[YHContributionFooterView alloc]initWithFrame:CGRectZero];
        _footerView.hidden = YES;
    }
    return _footerView;
}

-(NSMutableArray *)dataSource{
    if(_dataSource == nil){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
@end
